package ${baseInfo.packageName};

import com.fa4j.common.base.exception.CommonRequestError;
import com.fa4j.common.base.model.Page;
import com.fa4j.common.base.model.RemoveCmd;
import com.fa4j.common.web.annotation.ApiMethod;
import com.fa4j.common.web.model.AppType;
import ${domain.packageName}.${domain.fileName};
import ${createCmd.packageName}.${createCmd.fileName};
import ${modifyCmd.packageName}.${modifyCmd.fileName};
import ${pageQuery.packageName}.${pageQuery.fileName};
import ${repositoryInterface.packageName}.${repositoryInterface.fileName};
import ${serviceInterface.packageName}.${serviceInterface.fileName};
import com.fa4j.system.login.util.LoginUtil;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

<#assign parts = baseInfo.packageName?split(".")>
<#if parts?size gte 4>
    <#assign moduleName = parts[2]>
    <#assign entityName = parts[3]>
    <#if entityName == tableClass.shortClassName?uncap_first>
        <#assign path = moduleName+"/"+entityName>
    <#else>
        <#assign path = moduleName+"/"+entityName+"/"+tableClass.shortClassName?uncap_first>
    </#if>
<#else>
    <#assign path = tableClass.shortClassName?uncap_first>
</#if>
/**
 * ${tableClass.remark} Web接口
 *
 * @since ${.now?string('yyyy-MM-dd HH:mm:ss')}
 */
@RestController
@RequestMapping("${path}")
@RequiredArgsConstructor
public class ${baseInfo.fileName} {

    private final ${serviceInterface.fileName} ${serviceInterface.fileName?uncap_first};
    private final ${repositoryInterface.fileName} ${repositoryInterface.fileName?uncap_first};

    @GetMapping("page")
    @ApiMethod(name = "${tableClass.remark}-分页查询", type = AppType.MANAGE)
    public Page<${domain.fileName}> page(${pageQuery.fileName} query) {
        return ${repositoryInterface.fileName?uncap_first}.queryPage(query);
    }

    @GetMapping("{id}")
    @ApiMethod(name = "${tableClass.remark}-详情查询", type = AppType.MANAGE)
    public ${domain.fileName} detail(@PathVariable ${tableClass.pkFields[0].shortTypeName} id) {
        return ${repositoryInterface.fileName?uncap_first}.findById(id)
                .orElseThrow(() -> CommonRequestError.NOT_FOUND.build("${tableClass.remark}"));
    }

    @PostMapping
    @ApiMethod(name = "${tableClass.remark}-创建", type = AppType.MANAGE)
    public ${domain.fileName} create(@Valid @RequestBody ${createCmd.fileName} cmd) {
        return ${serviceInterface.fileName?uncap_first}.create(cmd);
    }

    @PatchMapping
    @ApiMethod(name = "${tableClass.remark}-编辑", type = AppType.MANAGE)
    public ${domain.fileName} modify(@Valid @RequestBody ${modifyCmd.fileName} cmd) {
        return ${serviceInterface.fileName?uncap_first}.modify(cmd);
    }

    @DeleteMapping("{id}")
    @ApiMethod(name = "${tableClass.remark}-删除", type = AppType.MANAGE)
    public ${domain.fileName} remove(@PathVariable ${tableClass.pkFields[0].shortTypeName} id) {
        return ${serviceInterface.fileName?uncap_first}.remove(RemoveCmd.of(id, LoginUtil.getAccountId()));
    }

}
